var _id;
var _pwd;

$(init);

function init() {
    _id = cookie('id');
    _pwd = cookie('password');
    $('body').on('click', '#change-pwd-btn', doSave);
}

function doSave(e) {
    var oldPwd = $('#oldPwd').val();
    var newPwd = $('#newPwd').val();
    var cnfPwd = $('#cnfPwd').val();

    if(_pwd != oldPwd) {
        notifyToast('输入旧密码有误！');
        $('div.old-pwd .error').css('display','inline-block');
        $('div.old-pwd .success').css('display','none');
    }else if (newPwd == '') {
        notifyToast('请输入密码！');
        $('div.new-pwd .error').css('display','inline-block');
        $('div.new-pwd .success').css('display','none');
    }else if (newPwd != cnfPwd) {
        notifyToast('两次输入密码不匹配！');
        $('div.cnf-pwd .error').css('display','inline-block');
        $('div.cnf-pwd .success').css('display','none');
    }else {
        $('div.center .success').css('display','inline-block');
        $('div.center .error').css('display','none');
        data = {
            oldPassword: oldPwd,
            password: newPwd
        };
        promise('POST',URL_SELL_PASSWD,JSON.stringify(data), cbSave, null);
    }
}

function cbSave(e) {
    notifyToast(MSG_UPDATE_SUCCESS);
    setTimeout(function () {
        Cookies.remove('cko')
        goto('/mobile.html');
    }, 1000);
 }